package com.hengxinguotong.zhihuichengjian.ui.safetystudy;

import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import com.hengxinguotong.zhihuichengjian.R;
import com.hengxinguotong.zhihuichengjian.bean.Person;
import com.hengxinguotong.zhihuichengjian.bean.PersonDetailRes;
import com.hengxinguotong.zhihuichengjian.ui.BaseActivity;
import com.hengxinguotong.zhihuichengjian.utils.SPUtil;
import com.hengxinguotong.zhihuichengjian.utils.Utils;
import com.hengxinguotong.zhihuichengjian.widget.font.HXTextView;
import com.hengxinguotong.zhihuichengjian.widget.view.CircleImageView;
import com.lidroid.xutils.http.RequestParams;

import android.os.Bundle;
import android.util.Log;
import android.view.View;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @CopyRight 恒信国通信息科技
 * @Author WuLei
 * @Date 2018/10/31
 */

public class PersonDetailActivity extends BaseActivity implements View.OnClickListener
{
    private static final String TAG = "PersonDetailActivity";
    @BindView(R.id.title_tv)
    HXTextView titleTv;
    @BindView(R.id.head_portrait_img)
    CircleImageView headPortraitImg;
    @BindView(R.id.name_tv)
    HXTextView nameTv;
    @BindView(R.id.sex_tv)
    HXTextView sexTv;
    @BindView(R.id.birth_day_tv)
    HXTextView birthDayTv;
    @BindView(R.id.card_type_tv)
    HXTextView cardTypeTv;
    @BindView(R.id.card_num_tv)
    HXTextView cardNumTv;
    @BindView(R.id.address_detail_tv)
    HXTextView addressDetailTv;
    @BindView(R.id.mobile_number_tv)
    HXTextView mobileNumberTv;
    @BindView(R.id.education_tv)
    HXTextView educationTv;
    @BindView(R.id.nation_tv)
    HXTextView nationTv;
    @BindView(R.id.native_place_tv)
    HXTextView nativePlaceTv;
    @BindView(R.id.employee_num_tv)
    HXTextView employeeNumTv;
    @BindView(R.id.unit_tv)
    HXTextView unitTv;
    @BindView(R.id.position_tv)
    HXTextView positionTv;
    @BindView(R.id.work_type_tv)
    HXTextView workTypeTv;
    @BindView(R.id.integral_tv)
    HXTextView integralTv;
    @BindView(R.id.level_tv)
    HXTextView levelTv;
    @BindView(R.id.health_tv)
    HXTextView healthTv;
    @BindView(R.id.guard_num_tv)
    HXTextView guardNumTv;
    @BindView(R.id.regist_date_tv)
    HXTextView registDateTv;
    @BindView(R.id.enter_date_tv)
    HXTextView enterDateTv;
    @BindView(R.id.is_leave_tv)
    HXTextView isLeaveTv;
    @BindView(R.id.study_time_tv)
    HXTextView studyTimeTv;
    @BindView(R.id.study_count_tv)
    HXTextView studyCountTv;

    private Person person;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_person_detail);
        ButterKnife.bind(this);
        person = (Person) getIntent().getSerializableExtra("person");
        initView();
        queryPersonDetail();
    }

    private void initView(){
        titleTv.setText(R.string.person_detail);
    }

    private void queryPersonDetail(){
        RequestParams params = new RequestParams();
        params.addBodyParameter("id", person.getId());
        Utils.requestData("加载中…", this, "/thridPerson/queryPersonDetail/" + SPUtil.getString(this,"uuid"), params, new Utils.ListenerJson() {@Override
        public void onSuccess(String json) {
            if(!Utils.isEmpty(json)){
                Gson gson = new Gson();
                PersonDetailRes detailRes = gson.fromJson(json, PersonDetailRes.class);
                if(detailRes.getStatus() == 1){
                    Person mPerson = detailRes.getValue();
                    if(mPerson == null){
                        showToast(R.string.empty_person);
                    }else{
                        process(mPerson);
                    }
                }else{
                    showToast(detailRes.getMsg());
                }
            }else{
                showToast(R.string.system_erro);
            }

        }

            @Override
            public void onFailure(String json) {
                Log.e(TAG, "CarManagementActivity===>>>" + json);
            }
        });
    }

    private void process(Person mPerson){
        Glide.with(this)
                .load(mPerson.getFileName())
                .placeholder(R.mipmap.default_head_portrait)
                .error(R.mipmap.default_head_portrait)
                .into(headPortraitImg);
        nameTv.setText(mPerson.getPersonName());
        sexTv.setText(mPerson.getSex());
        birthDayTv.setText(mPerson.getBirthDay());
        cardTypeTv.setText(mPerson.getStandby7());
        cardNumTv.setText(mPerson.getIdentifyID());
        addressDetailTv.setText(mPerson.getAddress());
        mobileNumberTv.setText(mPerson.getTelPhone());
        educationTv.setText(mPerson.getDegrees());
        nationTv.setText(mPerson.getNation());
        nativePlaceTv.setText(mPerson.getNatives());
        employeeNumTv.setText(mPerson.getTraID());
        unitTv.setText(mPerson.getUnitID());
        positionTv.setText(mPerson.getStation());
        workTypeTv.setText(mPerson.getCategory());
        integralTv.setText(mPerson.getIntegral());
        levelTv.setText(mPerson.getCategoryLevel());
        healthTv.setText(mPerson.getHealthReport());
        guardNumTv.setText(mPerson.getReadCardId());
        registDateTv.setText(mPerson.getRegisterDate());
        enterDateTv.setText(mPerson.getEntranceDate());
        isLeaveTv.setText(mPerson.getIsOut());
        if(!Utils.isEmpty(mPerson.getTrainCount())){
            studyCountTv.setText( mPerson.getTrainCount() + "次");
        }
        studyTimeTv.setText(mPerson.getTrainHour());
    }

    @OnClick({R.id.iv_back})
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.iv_back:
                finish();
                break;
        }
    }
}
